package com.example.demo.queue.producer;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.queue.message.LogEvent;
import com.lmax.disruptor.RingBuffer;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class LogEventProducer {
    private final RingBuffer<LogEvent> ringBuffer;

    public void insertLog(JSONObject logInfo) {
        long sequence = ringBuffer.next();
        try {
            LogEvent logEvent = ringBuffer.get(sequence);
            logEvent.setLogId(logInfo.getString("logId"));
            logEvent.setUserid(logInfo.getString("userid"));
            logEvent.setName(logInfo.getString("name"));
            logEvent.setUrl(logInfo.getString("url"));
            logEvent.setIp(logInfo.getString("ip"));
            logEvent.setContentType(logInfo.getString("contentType"));
            logEvent.setMethod(logInfo.getString("method"));
            logEvent.setVisitTime(logInfo.getString("visitTime"));
            logEvent.setEndTime(logInfo.getString("endTime"));
            logEvent.setWaitTime(logInfo.getString("waitTime"));
            logEvent.setParam(logInfo.getString("param"));
            logEvent.setResult(logInfo.getString("result"));
            logEvent.setMessage(logInfo.getString("message"));
            logEvent.setReturnType(logInfo.getString("returnType"));
        } finally {
            // 发布事件到队列中
            ringBuffer.publish(sequence);
        }
    }
}
